/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PeriodicTable.java
 *
 * Created on 18.11.2009, 18:42:29
 */

package pabudr_se;

import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
 *
 * @author paff
 */
public class PeriodicTable extends javax.swing.JDialog {

    /** Creates new form PeriodicTable */
        private Atom[] atom;
        private Object[][] output;
        private dbConnection db;
        private AtomCfgPanel acp;

    /** Creates new form PeriodensystemPopup */
    public PeriodicTable(java.awt.Frame parent, boolean modal, AtomCfgPanel acp) {
        super(parent, modal);
        this.acp=acp;
        try {
            db = new dbConnection();
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        }
        initComponents();
    }


    	public Object[][] getAtomTable(){
		try {
			atom = db.getAlleAtome();
			int count=0;
			output = new Object[atom.length/4+1][8];
			for(int i=0; i<atom.length; i+=4)
                        {
				output[count][0]=false;
				output[count][1]=atom[i].getName();
				output[count][2]=false;
				output[count][3]=atom[i+1].getName();
				if(i+2==atom.length)
                                {
					output[count][4]=null;
					output[count][5]=null;
					output[count][6]=null;
					output[count][7]=null;
					break;
				} // End of "if(i+2==atom.length)"
				output[count][4]=false;
				output[count][5]=atom[i+2].getName();
				output[count][6]=false;
				output[count++][7]=atom[i+3].getName();
			} // End of "for(int i=0; i<atom.length; i+=4)"
                        
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return output;
	}


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buUebernehmen = new javax.swing.JButton();
        tfMasse = new javax.swing.JTextField();
        tfRadius = new javax.swing.JTextField();
        buAbbrechen = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        PeriodicTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(pabudr_se.Pabudr_seApp.class).getContext().getResourceMap(PeriodicTable.class);
        buUebernehmen.setText(resourceMap.getString("buUebernehmen.text")); // NOI18N
        buUebernehmen.setName("buUebernehmen"); // NOI18N
        buUebernehmen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buUebernehmenActionPerformed(evt);
            }
        });

        tfMasse.setName("tfMasse"); // NOI18N

        tfRadius.setName("tfRadius"); // NOI18N

        buAbbrechen.setText(resourceMap.getString("buAbbrechen.text")); // NOI18N
        buAbbrechen.setName("buAbbrechen"); // NOI18N
        buAbbrechen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buAbbrechenActionPerformed(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        PeriodicTable.setModel(new javax.swing.table.DefaultTableModel(
            getAtomTable(),
            new String [] {
                "Anzahl", "Atom", "Anzahl1", "Atom1", "Anzahl2", "Atom2", "Anzahl3", "Atom3",
            })
            {
                Class[] types = new Class [] {
                    java.lang.Boolean.class, java.lang.String.class, java.lang.Boolean.class, java.lang.String.class, java.lang.Boolean.class, java.lang.String.class, java.lang.Boolean.class, java.lang.String.class
                };
                boolean[] canEdit = new boolean [] {
                    true, false, true, false, true, false, true, false
                };
                public Class getColumnClass(int columnIndex) {
                    return types [columnIndex];
                }

                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return canEdit [columnIndex];
                }
            }
        );
        PeriodicTable.getColumn("Anzahl").setMinWidth(30);
        PeriodicTable.getColumn("Anzahl").setMaxWidth(30);
        PeriodicTable.getColumn("Atom").setMinWidth(100);
        PeriodicTable.getColumn("Atom").setMaxWidth(100);
        PeriodicTable.getColumn("Anzahl1").setMinWidth(30);
        PeriodicTable.getColumn("Anzahl1").setMaxWidth(30);
        PeriodicTable.getColumn("Atom1").setMinWidth(100);
        PeriodicTable.getColumn("Atom1").setMaxWidth(100);
        PeriodicTable.getColumn("Anzahl2").setMinWidth(30);
        PeriodicTable.getColumn("Anzahl2").setMaxWidth(30);
        PeriodicTable.getColumn("Atom2").setMinWidth(100);
        PeriodicTable.getColumn("Atom2").setMaxWidth(100);
        PeriodicTable.getColumn("Anzahl3").setMinWidth(30);
        PeriodicTable.getColumn("Anzahl3").setMaxWidth(30);
        PeriodicTable.getColumn("Atom3").setMinWidth(100);
        PeriodicTable.getColumn("Atom3").setMaxWidth(100);
        PeriodicTable.setColumnSelectionAllowed(true);
        PeriodicTable.setName("Periodic Table");
        PeriodicTable.setTableHeader(null);
        PeriodicTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                PeriodicTableMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                PeriodicTableMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(PeriodicTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 525, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(tfRadius, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(tfMasse, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(buUebernehmen)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addComponent(buAbbrechen)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 348, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfRadius, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(tfMasse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buAbbrechen)
                    .addComponent(buUebernehmen))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buUebernehmenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buUebernehmenActionPerformed
        acp.setAtomData(getSelectedAtom());
      //  this.dispose();
}//GEN-LAST:event_buUebernehmenActionPerformed

    private void buAbbrechenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buAbbrechenActionPerformed
        this.dispose();
}//GEN-LAST:event_buAbbrechenActionPerformed

    private void PeriodicTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_PeriodicTableMouseClicked
        
}//GEN-LAST:event_PeriodicTableMouseClicked

    private void PeriodicTableMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_PeriodicTableMousePressed
        int index = getIndex();
        tfRadius.setText(atom[index].getRadius()+"");
        tfMasse.setText(atom[index].getMasse()+"");
    }//GEN-LAST:event_PeriodicTableMousePressed

    private int getIndex() {
		int spalte = 0;
                if(PeriodicTable.getSelectedColumn()==0) ;
		if(PeriodicTable.getSelectedColumn()==1) ;
                if(PeriodicTable.getSelectedColumn()==2) spalte=1;
		if(PeriodicTable.getSelectedColumn()==3) spalte=1;
                if(PeriodicTable.getSelectedColumn()==4) spalte=2;
		if(PeriodicTable.getSelectedColumn()==5) spalte=2;
                if(PeriodicTable.getSelectedColumn()==6) spalte=3;
		if(PeriodicTable.getSelectedColumn()==7) spalte=3;
		int zeile = PeriodicTable.getSelectedRow();
		return zeile*4+spalte;
}

    private Atom getSelectedAtom(){
        Atom a=null;
        int count=0;
        for(int i=0; i<PeriodicTable.getRowCount(); i++){
            if((Boolean)PeriodicTable.getValueAt(i, 0)){
                a=atom[i*4+0];
                count+=1;
            }
            if((Boolean)PeriodicTable.getValueAt(i, 2)){
                a=atom[i*4+1];
                count+=1;
            }
            if((Boolean)PeriodicTable.getValueAt(i, 4)!=null && (Boolean)PeriodicTable.getValueAt(i, 4)){
                a=atom[i*4+2];
                count+=1;
            }
            if((Boolean)PeriodicTable.getValueAt(i, 4)!=null && (Boolean)PeriodicTable.getValueAt(i, 6)){
                a=atom[i*4+3];
                count+=1;
            }
        }
        if(count<2){
            this.dispose();
            return a;
        }
        JOptionPane.showMessageDialog(this, "Es darf maximal ein Element ausgewählt werden!", "Fehler", JOptionPane.ERROR_MESSAGE);
        return null;
    }

    /**
    * @param args the command line arguments
    */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable PeriodicTable;
    private javax.swing.JButton buAbbrechen;
    private javax.swing.JButton buUebernehmen;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField tfMasse;
    private javax.swing.JTextField tfRadius;
    // End of variables declaration//GEN-END:variables

}
